export default {
    'edc.excelView.tableInsertCombineRow': function () {
        const sheet = this.workbook.getActiveSheet();
        let row = sheet.getActiveRowIndex();
        const col = sheet.getActiveColumnIndex();
        const table = sheet.tables.find(row, col);
        const selections = sheet.getSelections();
        const { rowCount: selectionRowCount } = selections[0];
        if (table && table.bindingPath()) {
            const actionName = 'edc.excelView.tableInsertCombineRow';
            const span = sheet.getSpan(row, col);
            if (span) {
                row = span.row + span.rowCount - 1;
            }
            this.workbook.commandManager().execute({
                autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                cmd: actionName,
                sheet,
                sheetName: sheet.name(),
                row,
                col,
                table,
                store: this
            });
            this.setUserinfo2newRow(table, row + 1, 1)
        }
    },
    'edc.excelView.tableInsertTopRow': function () {
        const sheet = this.workbook.getActiveSheet();
        let row = sheet.getActiveRowIndex();
        const col = sheet.getActiveColumnIndex();
        const table = sheet.tables.find(row, col);
        const selections = sheet.getSelections();
        let { rowCount: selectionRowCount } = selections[0];

        if (table) {
            const { row: startRow, col: startCol, rowCount, colCount } = table.dataRange();

            if (table.bindingPath() && row > startRow) {
                for (let index = col - 1; index >= startCol; index--) {
                    const span = sheet.getSpan(row, index);
                    if (span && span.row < row) {
                        const actionName = 'edc.excelView.tableInsertCombineRow';
                        this.workbook.commandManager().execute({
                            autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                            cmd: actionName,
                            sheet,
                            sheetName: sheet.name(),
                            row,
                            insertType: 'top',
                            col: span.col,
                            table,
                            store: this,
                            insertRowCount: selectionRowCount
                        });
                        this.setUserinfo2newRow(table, row, selectionRowCount)
                        return;
                    }
                }
            }

            let actionName = 'edc.excelView.tableInsertRow';
            if (table.name().startsWith("custom")) {
                this.resetTableSpan(table);
                actionName = 'edc.excelView.custableInsertRow';
                let count;
                for (let i = 0; i < colCount; i++) {
                    const c = startCol + i;
                    const span = sheet.getSpan(row, c);
                    let spanrow;
                    let spancount;
                    if (span) {
                        spanrow = span.row;
                        spancount = span.rowCount;
                        //if(!count || span.rowCount < count )
                    } else {
                        spanrow = row;
                        spancount = 1;
                    }
                    const celltype = sheet.getCell(spanrow, c).cellType();
                    // 新增的行数为向上扩展的最小单位
                    if (celltype && celltype.expandType == '2') {
                        if (!count || spancount < count) {
                            count = spancount;
                            row = spanrow;
                        }

                    }


                }

                selectionRowCount = count ? count : 1;
            }
            this.workbook.commandManager().execute({
                autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                cmd: actionName,
                sheet,
                sheetName: sheet.name(),
                row,
                insertType: 'top',
                table,
                store: this,
                insertRowCount: selectionRowCount
            });
            this.setUserinfo2newRow(table, row, selectionRowCount)
        }
    },
    'edc.excelView.tableInsertBottomRow': function (workbook, paramRow, paramCol) {
        const sheet = this.workbook.getActiveSheet();
        let row = paramRow ? paramRow : sheet.getActiveRowIndex();
        const col = paramCol ? paramCol : sheet.getActiveColumnIndex();
        const table = sheet.tables.find(row, col);
        const selections = sheet.getSelections();
        let { rowCount: selectionRowCount } = selections[0];

        if (table) {
            const { row: startRow, col: startCol, rowCount, colCount } = table.dataRange();
            const span = sheet.getSpan(row, col);
            let rangeRowCount = 1;
            //if (span) {
            //row = row + span.rowCount - 1;
            //row = row - selectionRowCount + 1; 
            //}

            if (table.bindingPath() && row < startRow + rowCount - 1) {
                for (let index = col - 1; index >= startCol; index--) {
                    const span = sheet.getSpan(row, index);
                    if (span && ((span.row + span.rowCount) > (row + 1))) {
                        const actionName = 'edc.excelView.tableInsertCombineRow';

                        this.workbook.commandManager().execute({
                            autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                            cmd: actionName,
                            sheet,
                            sheetName: sheet.name(),
                            row,
                            insertType: 'bottom',
                            col: span.col,
                            table,
                            store: this,
                            insertRowCount: selectionRowCount
                        });
                        this.setUserinfo2newRow(table, row + 1, selectionRowCount)
                        return;
                    }
                }
            }
            let actionName = 'edc.excelView.tableInsertRow';
            if (table.name().startsWith("custom")) {
                this.resetTableSpan(table);
                actionName = 'edc.excelView.custableInsertRow';
                let count;
                for (let i = 0; i < colCount; i++) {
                    const c = startCol + i;
                    const span = sheet.getSpan(row, c);
                    let spanrow;
                    let spancount;
                    if (span) {
                        spanrow = span.row;
                        spancount = span.rowCount;
                        //if(!count || span.rowCount < count )
                    } else {
                        spanrow = row;
                        spancount = 1;
                    }
                    const celltype = sheet.getCell(spanrow, c).cellType();
                    // 新增的行数为向下扩展的最小单位
                    if (celltype && celltype.expandType == '2') {
                        if (!count || spancount < count) {
                            count = spancount;
                            row = spanrow;
                        }

                    }


                }

                selectionRowCount = count ? count : 1;
            }
            this.workbook.commandManager().execute({
                autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                cmd: actionName,
                sheet,
                sheetName: sheet.name(),
                row,
                insertType: 'bottom',
                table,
                store: this,
                insertRowCount: selectionRowCount
            });
            this.setUserinfo2newRow(table, row + selectionRowCount, selectionRowCount)
        }
    },
    'edc.excelView.tableDeleteRow': function (workbook, paramsRow, paramsCol) {
        const sheet = this.workbook.getActiveSheet();
        let row = paramsRow || sheet.getActiveRowIndex()
        const col = paramsCol || sheet.getActiveColumnIndex()
        const table = sheet.tables.find(row, col);
        const selections = sheet.getSelections();
        let { rowCount: selectionRowCount } = selections[0];
        if (table) {
            const { row: startRow, col: startCol, rowCount, colCount } = table.dataRange();
            let actionName = 'edc.excelView.tableDeleteRow';
            if (table.name().startsWith("custom")) {
                this.resetTableSpan(table);
                actionName = 'edc.excelView.custableDeleteRow';
                let count;
                for (let i = 0; i < colCount; i++) {
                    const c = startCol + i;
                    const span = sheet.getSpan(row, c);
                    let spanrow;
                    let spancount;
                    if (span) {
                        spanrow = span.row;
                        spancount = span.rowCount;
                        //if(!count || span.rowCount < count )
                    }
                    const celltype = sheet.getCell(spanrow, c).cellType();
                    // 新增的行数为向下扩展的最小单位
                    if (celltype && celltype.expandType == '2') {
                        if (!count || spancount < count) {
                            count = spancount;
                            row = spanrow;
                        }

                    }

                }

                selectionRowCount = count ? count : 1;
            }
            this.workbook.commandManager().execute({
                autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                cmd: actionName,
                sheet,
                sheetName: sheet.name(),
                row: row,
                table,
                store: this,
                deleteRowCount: selectionRowCount
            });
        }
    },
    'edc.excelView.custableDeleteCol': function (workbook, paramsRow, paramsCol) {
        const sheet = this.workbook.getActiveSheet();
        let row = paramsRow || sheet.getActiveRowIndex()
        const col = paramsCol || sheet.getActiveColumnIndex()
        const table = sheet.tables.find(row, col);
        const selections = sheet.getSelections();
        let { rowCount: selectionColCount } = selections[0];
        if (table && table.name().startsWith("custom")) {
            this.resetTableSpan(table);
            const { row: startRow, col: startCol, rowCount, colCount } = table.dataRange();
            let actionName = 'edc.excelView.custableDeleteCol';
            // let count;
            // for(let i = 0 ; i < colCount ; i++) {
            //     const c = startCol + i;
            //     const span = sheet.getSpan(row, c);
            //     let spanrow;
            //     let spancount;
            //     if(span) {
            //         spanrow = span.row;
            //         spancount = span.rowCount;
            //         //if(!count || span.rowCount < count )
            //     }
            //     const celltype = sheet.getCell(spanrow,c).cellType();
            //     // 新增的行数为向下扩展的最小单位
            //     if(celltype && celltype.expandType == '2') {
            //         if(!count || spancount < count ) {
            //             count = spancount;
            //             row = spanrow;
            //         }

            //     }

            // }

            selectionColCount = 1;

            this.workbook.commandManager().execute({
                autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                cmd: actionName,
                sheet,
                sheetName: sheet.name(),
                col: col,
                table,
                store: this,
                deleteColCount: selectionColCount
            });
        }
    },
    'edc.excelView.custableInsertLeftCol': function (workbook, paramsRow, paramsCol) {
        const sheet = this.workbook.getActiveSheet();
        let row = paramsRow || sheet.getActiveRowIndex()
        const col = paramsCol || sheet.getActiveColumnIndex()
        const table = sheet.tables.find(row, col);
        const selections = sheet.getSelections();
        let { rowCount: selectionColCount } = selections[0];
        if (table && table.name().startsWith("custom")) {
            this.resetTableSpan(table);
            const { row: startRow, col: startCol, rowCount, colCount } = table.dataRange();
            let actionName = 'edc.excelView.custableInsertCol';
            let count;
            let expandCol;
            let expandStartRow;
            for (let i = 0; i < colCount; i++) {
                const c = startCol + i;
                const span = sheet.getSpan(row, c);
                let spanrow;
                let spancount;
                if (span) {
                    spanrow = span.row;
                    spancount = span.rowCount;
                    //if(!count || span.rowCount < count )
                } else {
                    spanrow = row;
                    spancount = 1;
                }
                const celltype = sheet.getCell(spanrow, c).cellType();
                // 新增的行数为向下扩展的最小单位
                if (celltype && celltype.expandType == '2') {
                    if (!count || spancount < count) {
                        count = spancount;
                        expandCol = c;
                        row = spanrow;
                    }

                }

            }


            count = count ? count : 1;
            for (let r = row; r >= startRow; r = r - count) {
                const cell = sheet.getCell(r, expandCol);
                const cellType = cell.cellType();
                if ( cellType && cellType.expandType == '2') {
                    expandStartRow = r;
                }
            }

            selectionColCount = 1;

            this.workbook.commandManager().execute({
                autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                cmd: actionName,
                sheet,
                sheetName: sheet.name(),
                col: col,
                table,
                insertType: 'left',
                store: this,
                downExpandCount: count, // 纵向扩展最小单位
                expandStartRow, // 首个循环纵向扩展行
                insertColCount: selectionColCount
            });
            this.setUserinfo2newCol(table, col, selectionColCount)
        }
    },
    'edc.excelView.custableInsertRightCol': function (workbook, paramsRow, paramsCol) {
        const sheet = this.workbook.getActiveSheet();
        let row = paramsRow || sheet.getActiveRowIndex()
        const col = paramsCol || sheet.getActiveColumnIndex()
        const table = sheet.tables.find(row, col);
        const selections = sheet.getSelections();
        let { rowCount: selectionColCount } = selections[0];
        if (table && table.name().startsWith("custom")) {
            this.resetTableSpan(table);
            const { row: startRow, col: startCol, rowCount, colCount } = table.dataRange();
            let actionName = 'edc.excelView.custableInsertCol';
            let count;
            let expandCol;
            let expandStartRow;
            for (let i = 0; i < colCount; i++) {
                const c = startCol + i;
                const span = sheet.getSpan(row, c);
                let spanrow;
                let spancount;
                if (span) {
                    spanrow = span.row;
                    spancount = span.rowCount;
                    //if(!count || span.rowCount < count )
                } else {
                    spanrow = row;
                    spancount = 1;
                }
                const celltype = sheet.getCell(spanrow, c).cellType();
                // 新增的行数为向下扩展的最小单位
                if (celltype && celltype.expandType == '2') {
                    if (!count || spancount < count) {
                        count = spancount;
                        expandCol = c;
                        row = spanrow;
                    }

                }

            }


            count = count ? count : 1;
            for (let r = row; r >= startRow; r = r - count) {
                const cell = sheet.getCell(r, expandCol);
                const cellType = cell.cellType();
                if (cellType && cellType.expandType == '2') {
                    expandStartRow = r;
                }
            }

            selectionColCount = 1;

            this.workbook.commandManager().execute({
                autoFitType: GC.Spread.Sheets.AutoFitType.cell,
                cmd: actionName,
                sheet,
                sheetName: sheet.name(),
                col: col,
                table,
                insertType: 'right',
                store: this,
                downExpandCount: count, // 纵向扩展最小单位
                expandStartRow, // 首个循环纵向扩展行
                insertColCount: selectionColCount
            });
            this.setUserinfo2newCol(table, col + selectionColCount, selectionColCount);
        }
    }
}